<template>
	<view class="u-p-30">
		<view class="flex-center">
			<text class="u-shrink-no">反馈类型</text>
			<view class="flex-around u-m-l-30 u-width-100">
				<view :class="current==i?'active':'backtype'" @click="changetab(i)" v-for="(i,index) in typelist">
					{{i}}
				</view>
			</view>
		</view>
		<view class="">
			<u--textarea class="textbox" v-model="content" maxlength=200 height=200 placeholder="请简单描述你的问题" count></u--textarea>
		</view>
		<view class="">
			上传图片
		</view>
		<u-upload class="u-m-t-30" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
			:maxCount="10"></u-upload>
		<button @click="submit" class="btn" >提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				typelist: ['改善', 'bug', '其他'],
				current: null,
				sublist:[],
				content:''
			};
		},
		methods: {
			changetab(item) {
				this.current = item
			},
			submit(){
				if(!this.current){
					return this.$toast('请选择类型~')
				}
				if(!this.content){
					return this.$toast('请输入你的问题~')
				}
				
				this.$api.post('/feedback/addForWechat',{
					file:this.sublist||'',
					content:this.content,
					type:this.current
				}).then(res=>{
					this.$toast(res.msg)
					this.content = ''
					this.fileList1 = []
					this.sublist = []
				})
				
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
				this.sublist.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "http://47.108.227.85:8700/minio/upfile/upFileForWeChat", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.msg);
							}, 1000);
							this.sublist.push(JSON.parse(res.data).msg)
						},
					});
				});
			},

		}
	}
</script>

<style lang="scss" scoped>
	.active {
		background-color: #756E6E;
		padding: 8rpx 40rpx;
		border-radius: 24rpx;
		color: #fff;
	}

	.backtype {
		background-color: #E6E6E6;
		padding: 8rpx 40rpx;
		border-radius: 24rpx;
		color: #000;
	}

	.textbox {
		margin: 50rpx auto;
		width: 100%;
		box-sizing: border-box;
		padding: 20rpx;
		// background-color: #EFEFEF;
	}
	.btn{
		margin-top: 80rpx;
		background-color: #1856C4;
		color: #fff;
		border-radius: 40rpx;
	}
</style>